/*
 * Copyright (C) 2019 ~ 2022 Uniontech Software Technology Co.,Ltd.
 *
 * Author:     yanghongwei <yanghongwei@uniontech.com>
 *
 * Maintainer: yanghongwei <yanghongwei@uniontech.com>
 */

#include "abstractnotifyprocess.h"
#include "filenotify.h"

#include <iostream>

using namespace std;

AbstractNotifyProcess::AbstractNotifyProcess()
    : notifyType(I_Notify)
{
    // 初始化FileNotify
    FileNotify::instance()->init();
}

AbstractNotifyProcess::~AbstractNotifyProcess()
{
}

/**
 * @brief 事件通知回调函数
 * @param opTime 事件发生事件
 * @param file 文件全路径
 * @param process 操作文件的进程名称
 * @param events 文件操作事件
 * @return true:fanotify PERM操作允许  false:fanotify PERM操作阻断
 * @note 该回调接口在其它线程被调用。需要注意多线程数据竞争
 */
bool AbstractNotifyProcess::notifyCallBackFun(uint64_t opTime, std::string file, std::string process, uint32_t events)
{
    std::cout << "opTime:" << opTime << " file:" << file << " process:" << process << " events:" << events << std::endl;
    // fanotify PERM操作默认允许，需要具体阻断的子类具体实现
    return true;
}

/**
 * @brief 注册事件通知回调
 * @return 注册成功返回true，否则返回false
 */
bool AbstractNotifyProcess::registeredCallBack()
{
    // 添加一个业务处理对象
    return FileNotify::instance()->addProcess(this);
}

/**
 * @brief 取消注册事件通知回调
 * @return 取消成功返回true，否则返回false
 * @note 取消注册前先移除所监听的事件
 */
bool AbstractNotifyProcess::unRegisteredCallBack()
{
    return FileNotify::instance()->removeProcess(this);
}

/**
 * @brief 添加一个要监听的节点
 * @param node 监听的节点
 * @return 添加成功返回true,否则返回false
 */
bool AbstractNotifyProcess::addNode(const NotifyNode &node)
{
    NotifyNode tmp = node;
    tmp.object = this;
    return FileNotify::instance()->addNode(tmp, notifyType);
}

/**
 * @brief 移除一个监听的节点
 * @param node 监听的节点
 * @return 移除成功返回true,否则返回false
 */
bool AbstractNotifyProcess::removeNode(const NotifyNode &node)
{
    NotifyNode tmp = node;
    tmp.object = this;
    return FileNotify::instance()->removeNode(tmp, notifyType);
}
